package com.ruoyi.web.controller.system;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;
import com.ruoyi.system.domain.SysDeviceState;
import com.ruoyi.system.domain.SysDeviceStateFile;
import com.ruoyi.system.domain.SysMarketFile;
import com.ruoyi.system.service.ISysDeviceStateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "设备状态确定")
@RestController
@RequestMapping("/device/state")
public class SysDeviceStateController  extends BaseController{

    @Autowired
    private ISysDeviceStateService sysDeviceStateService;

    @Autowired
    private ILifManageListService lifManageListService;


    @Autowired
    private ServerConfig serverConfig;


    /**
     * 查询设备状态确定列表
     */
    @ApiOperation(value = "查询设备状态确定列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:list')")
    @GetMapping("/getInfoList/{deviceId}")
    public AjaxResult getInfoList(@PathVariable("deviceId") Long deviceId)
    {
        LifManageList list = lifManageListService.selectLifManageList(deviceId);
        return AjaxResult.success(list);
    }

    /**
     * 查询设备状态确定列表
     */
    @ApiOperation(value = "查询设备状态确定列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:list')")
    @GetMapping("/list/{deviceId}")
    public AjaxResult list(@PathVariable("deviceId") Long deviceId,HttpServletRequest request)
    {
        List<SysDeviceState> list = sysDeviceStateService.selectSysDeviceStateList(deviceId,request);
        return AjaxResult.success(list);
    }

    /**
     * 获取项目Id查询设备状态确定
     */
    @ApiOperation(value = "获取项目Id查询详细信息")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:query')")
    @GetMapping(value = "/listBy")
    public TableDataInfo getInfoDeviceList(@RequestParam("id") Long id)
    {
        startPage();
        return getDataTable(sysDeviceStateService.selectDeviceStateServiceById(id));
    }

    /**
     * 根据文件Id 获取对应文件
     */
    @ApiOperation(value = "根据文件Id 获取对应文件")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:getInfo')")
    @GetMapping(value = "/listBy/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        List<SysMarketFile> list =sysDeviceStateService.selectDocFileByFileId(id);
        return AjaxResult.success(list);
    }


    /**
     * 新增设备状态确定列表
     */
    @ApiOperation(value = "新增设备状态确定列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:add')")
    @Log(title = "新增设备状态确定列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysDeviceState sysDeviceState, HttpServletRequest request)
    {
        sysDeviceState.setCreateBy(getUsername());
        sysDeviceState.setPerson(getUsername());
        return toAjax(sysDeviceStateService.insertSysDeviceState(request,sysDeviceState));
    }

    /**
     * 修改设备确定列表
     */
    @ApiOperation(value = "修改设备确定列表")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:updatebyid')")
    @Log(title = "修改设备确定列表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody SysDeviceState sysDeviceState)
    {
        return toAjax(sysDeviceStateService.updateSysDeviceState(sysDeviceState));
    }


    /**
     * 逻辑删除文件
     */
    @ApiOperation(value = "逻辑删除文件")
    @PreAuthorize("@ss.hasPermi('lifecycle:state:delete')")
    @Log(title = "逻辑删除文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileId}")
    public AjaxResult remove(@PathVariable Long fileId) {
        return toAjax(sysDeviceStateService.deleteMarketFileById(fileId));
    }


    /**
     * 上传请求
     */
    @ApiOperation(value = "实现文件上传")
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, @RequestParam("lifId") Long lifId) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            String name = file.getOriginalFilename();
            SysDeviceStateFile stateFile = new SysDeviceStateFile();
            stateFile.setDeviceId(lifId);
            stateFile.setFileName(name);
            stateFile.setFileUnique(fileName);
            stateFile.setFilePath(filePath);
            stateFile.setUrl(url);
            Long fileId = sysDeviceStateService.insertPathName(stateFile);
            ajax.put("name", name);
            ajax.put("fileId", fileId);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;

        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




}
